
// TestApplicationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestApplication.h"
#include "TestApplicationDlg.h"
#include <psapi.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTestApplicationDlg dialog

/*
13:16:22:74 --> <ENQ>
13:17:56:33 <-- <ACK>
13:17:56:35 --> <STX>1H|\^||HELQS2000W|^QuickScan 2000 WIN^Version 2.10 \18^SN 
13:17:57:35 --> 600300101|||<CR><ETX>F1<CR><LF>
13:17:57:66 <-- <ACK>
13:17:57:67 --> <STX>2Q|1|00001<CR>L|1<CR><ETX>B3<CR><LF>
13:17:58:02 <-- <ACK>
13:17:58:05 --> <EOT>
13:17:58:42 <-- <ENQ>
13:17:58:44 --> <ACK>
13:17:58:46 <-- <STX>1H|\^&||HELIETEST<CR><ETX>84<CR><LF>
13:17:58:88 --> <ACK>
13:17:58:89 <-- <STX>2P|1|||ROL905938|Rollins, Charles||1-11-1940|M|white||||Ki
13:17:59:94 <-- llian, T.|11||5' 10"|218||||||||Room 2843|||||||||<CR><ETX>A0<CR>
13:18:00:96 <-- <LF>
13:18:00:97 --> <ACK>
13:18:01:00 <-- 3L|1|N<CR><ETX>06<CR><LF><EOT>
13:18:16:02 --> <ENQ>
13:18:25:52 <-- <ACK>
13:18:25:53 --> <STX>1H|\^||HELQS2000W|^QuickScan 2000 WIN^Version 2.10 \18^SN 
13:18:26:60 --> 600300101|||<CR><ETX>F1<CR><LF>
13:18:26:91 <-- <ACK>
13:18:26:94 --> <STX>2Q|1|00002<CR>L|1<CR><ETX>B4<CR><LF>
13:18:27:31 <-- <ACK>
13:18:27:33 --> <EOT>
13:18:27:35 <-- <ENQ>
13:18:27:36 --> <ACK>
13:18:27:39 <-- <STX>1H|\^&||HELIETEST<CR><ETX>84<CR><LF>
13:18:27:83 --> <ACK>
13:18:27:85 <-- <STX>2P|1|||DOY132441|Doyle, Alice||10-12-1920|F|black||||Thorp
13:18:28:97 <-- e, C.|11||5' 4"|103||||||||Room 2120|||||||||<CR><ETX>23<CR><LF>
13:18:29:92 --> <ACK>
13:18:29:94 <-- <STX>3L|1|N<CR><ETX>06<CR><LF>
13:18:30:19 --> <ACK>
13:18:30:20 <-- <EOT>
13:18:38:16 --> <ENQ>
13:18:38:19 <-- <ACK>
13:18:38:20 --> <STX>1H|\^||HELQS2000W|^QuickScan 2000 WIN^Version 2.10 \18^SN 
13:18:39:33 --> 600300101|||<CR><ETX>F1<CR><LF>
13:18:39:67 <-- <ACK>
13:18:39:69 --> <STX>2Q|1|00003<CR>L|1<CR><ETX>B5<CR><LF>
13:18:40:09 <-- <ACK>
13:18:40:13 --> <EOT>
13:18:40:14 <-- <ENQ>
13:18:40:16 --> <ACK>
13:18:40:19 <-- <STX>1H|\^&||HELIETEST<CR><ETX>84<CR><LF>
13:18:40:66 --> <ACK>
13:18:40:67 <-- <STX>2P|1|||BER692788|Bernard, Charles||10-26-1966|M|black||||F
13:18:41:80 <-- ontenot, R.|11||5' 8"|174||||||||Room 2619|||||||||<CR><ETX>E7<CR>
13:18:42:86 <-- <LF>
13:18:42:89 --> <ACK>
13:18:42:91 <-- <STX>3L|1|N<CR><ETX>06<CR><LF>
13:18:43:16 --> <ACK>
13:18:43:17 <-- <EOT>
*/

#define CONTROL_ENQ '\x05'
#define CONTROL_ACK '\x06'
#define CONTROL_CR '\x0D'
#define CONTROL_LF '\x0A'
#define CONTROL_EOT '\x04'
#define CONTROL_STX '\x02'
#define CONTROL_ETX '\x03'

CTestApplicationDlg::CTestApplicationDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTestApplicationDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROCESSES, m_cboProcesses);
	DDX_Control(pDX, IDC_MODULE, m_lstModules);
}

BEGIN_MESSAGE_MAP(CTestApplicationDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CTestApplicationDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_SEND_TO_COM1, &CTestApplicationDlg::OnBnClickedButtonSendToCom1)
	ON_BN_CLICKED(IDC_BUTTON_WRITE_FILE, &CTestApplicationDlg::OnBnClickedButtonWriteFile)
	ON_BN_CLICKED(IDC_BUTTON_INTERCEPT, &CTestApplicationDlg::OnBnClickedButtonIntercept)
	ON_CBN_DROPDOWN(IDC_PROCESSES, &CTestApplicationDlg::OnCbnDropdownProcesses)
	ON_CBN_SELCHANGE(IDC_PROCESSES, &CTestApplicationDlg::OnCbnSelchangeProcesses)
END_MESSAGE_MAP()


// CTestApplicationDlg message handlers

BOOL CTestApplicationDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	HMODULE hModule2 = LoadLibraryA("C:\\vcl35.bpl");
	HMODULE hModule = LoadLibraryA("ComHook.dll");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestApplicationDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestApplicationDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTestApplicationDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	CDialogEx::OnOK();
}


void CTestApplicationDlg::OnBnClickedButtonSendToCom1()
{
	DWORD dwWritten;
	HANDLE hCom = CreateFileA("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	WriteFile(hCom, "Hello World", sizeof("Hello World"), &dwWritten, NULL);
	CloseHandle(hCom);
}


void CTestApplicationDlg::OnBnClickedButtonWriteFile()
{
}

void CTestApplicationDlg::OnBnClickedButtonIntercept()
{
	// TODO: Add your control notification handler code here
}

void CTestApplicationDlg::OnCbnDropdownProcesses()
{
	m_cboProcesses.ResetContent();
	DWORD dwaProcessIds[2048]; DWORD dwNeeded;
	EnumProcesses(dwaProcessIds, sizeof(dwaProcessIds), &dwNeeded);
	for (int i = 0; i < dwNeeded / sizeof(DWORD); i++)
	{
		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwaProcessIds[i]);
		if (hProcess)
		{
			char strFilename[MAX_PATH];
			HMODULE hMod;
			DWORD cbNeeded;

			if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
				if (GetModuleBaseName(hProcess, hMod, strFilename, sizeof(strFilename)))
				{
					int iIndex = m_cboProcesses.AddString(strFilename);
					m_cboProcesses.SetItemData(iIndex, dwaProcessIds[i]);
				}
			CloseHandle(hProcess);
		}
	}
}

void CTestApplicationDlg::OnCbnSelchangeProcesses()
{
	m_lstModules.ResetContent();

	DWORD dwProcessId = m_cboProcesses.GetItemData(m_cboProcesses.GetCurSel());
	HMODULE haMod[1024];
	DWORD cbNeeded;
	char strFilename[MAX_PATH];

	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
	if (hProcess)
	{
		if (EnumProcessModules(hProcess, haMod, sizeof(haMod) * sizeof(HMODULE), &cbNeeded))
			for (int i = 0; i < cbNeeded / sizeof(HMODULE); i++)
				if (GetModuleBaseName(hProcess, haMod[i], strFilename, sizeof(strFilename)))
				{
					m_lstModules.AddString(strFilename);
				}
	}
}
